﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Emergency
{
    public class Utils
    {
        public static uint ToAbgr(int a, int b, int g, int r)
        {
            uint color = (uint)(r | g << 8 | b << 16 | a << 24);
            return color;
        }

        public static uint ToAbgr(Color c)
        {
            int R = (int)c.R;
            int G = (int)c.G << 8;
            int B = (int)c.B << 16;
            int A = (int)(c.A == 0 ? 255 : c.A) << 24;

            uint color = (uint)(R | G | B | A);

            return color;
        }

        public static uint RGBA(int r, int g, int b, int a)
        {
            int ret = (r | (g << 8) | (b << 16) | (a << 24));
            return (uint)ret;
        }

        public static uint RGB(int r, int g, int b)
        {
            return (uint)(r | (g << 8) | (b << 16));
        }

        /// <summary>
        /// 透明度为255
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public static Color Abgr2Argb(int v)
        {
            Color c = Color.FromArgb(v);
            int A = (int)c.A;
            int R = (int)c.B;
            int G = (int)c.G;
            int B = (int)c.R;
            Color temp = Color.FromArgb(255, R, G, B);
            return temp;
        }

        /// <summary>
        /// 带真实透明度
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public static Color Abgr2Argb2(int v)
        {
            Color c = Color.FromArgb(v);
            int A = (int)c.A;
            int R = (int)c.B;
            int G = (int)c.G;
            int B = (int)c.R;
            Color temp = Color.FromArgb(A, R, G, B);
            return temp;
        }


        /// <summary>
        /// 格式ABGR
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Color HexNumberToColor(string value)
        {
            try
            {
                int a;
                int r;
                int g;
                int b;
                int.TryParse(value.Substring(0, 2), System.Globalization.NumberStyles.HexNumber, null, out a);
                int.TryParse(value.Substring(6, 2), System.Globalization.NumberStyles.HexNumber, null, out r);
                int.TryParse(value.Substring(4, 2), System.Globalization.NumberStyles.HexNumber, null, out g);
                int.TryParse(value.Substring(2, 2), System.Globalization.NumberStyles.HexNumber, null, out b);
                if (a == 0 && r == 0 && g == 0 && b == 0)
                    return Color.White;
                return Color.FromArgb(a, r, g, b);
            }
            catch (System.Exception e)
            {
                return Color.White;
            }
        }

        /// <summary>
        /// 格式ABGR,返回的颜色为rgb，a为255
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Color HexNumberToColorWithoutAlpha(string value)
        {
            try
            {
                int a;
                int r;
                int g;
                int b;
                int.TryParse(value.Substring(0, 2), System.Globalization.NumberStyles.HexNumber, null, out a);
                int.TryParse(value.Substring(6, 2), System.Globalization.NumberStyles.HexNumber, null, out r);
                int.TryParse(value.Substring(4, 2), System.Globalization.NumberStyles.HexNumber, null, out g);
                int.TryParse(value.Substring(2, 2), System.Globalization.NumberStyles.HexNumber, null, out b);
                if (a == 0 && r == 0 && g == 0 && b == 0)
                    return Color.White;
                return Color.FromArgb(255, r, g, b);
            }
            catch (System.Exception e)
            {
                return Color.White;
            }
        }

        /// <summary>
        /// 格式ABGR,返回的颜色为rgb，a为255
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int GetAlphaFromHexNumber(string value)
        {
            try
            {
                int a;
                int.TryParse(value.Substring(0, 2), System.Globalization.NumberStyles.HexNumber, null, out a);
                return a;
            }
            catch (System.Exception e)
            {
                return 255;
            }
        }

        /// <summary>
        ///  替换alpha
        /// </summary>
        /// <param name="value">16进制的颜色，abgr</param>
        /// <param name="alpha">不透明度，百分制</param>
        /// <returns></returns>
        public static string ReplaceAlphaInHexNumber(string value, double alpha)
        {
            try
            {
                int opacity = (int)Math.Floor(alpha * 255.0 / 100);
                string str = string.Format("{0:X2}{1}", opacity, value.Substring(2));
                return str;
            }
            catch (System.Exception e)
            {
                return "FFFFFFFF";
            }
        }

        /// <summary>
        /// 格式ABGR
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        public static string ColorToHexNumber(Color color)
        {
            string str = "";
            try
            {
                int a = (int)color.A;
                int r = (int)color.R;
                int g = (int)color.G;
                int b = (int)color.B;
                str = string.Format("{0:X2}{1:X2}{2:X2}{3:X2}", a, b, g, r);

            }
            catch (System.Exception ex)
            {
                str = "FFFFFFFF";
            }
            return str;
        }

        /// <summary>
        /// 格式ABGR
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        public static string ColorToHexNumber(Color color, byte alpha)
        {
            string str = "";
            try
            {
                int a = alpha;
                int r = (int)color.R;
                int g = (int)color.G;
                int b = (int)color.B;
                str = string.Format("{0:X2}{1:X2}{2:X2}{3:X2}", a, b, g, r);

            }
            catch (System.Exception ex)
            {
                str = "FFFFFFFF";
            }
            return str;
        } 
    }
}
